<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no,email=no" />
		<title>购物车提交订单页</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">提交订单</h1>
		</header>
		<div id="scrollDiv" class="scroll-div">
			<div class="mui-content transparent">
				<div id="orderSubmit" class="order-submit">
					
				</div>
			</div>
		</div>
		<div class="cart-bottom transparent">
			<p class="order-price-total">共<em id="OrderCount">0</em>件，总金额<span id="OrderAmount"></span></p>
			<a class="submit-cart" id="submitBtn">提交订单</a>
		</div>
		<div id="dcontent" class="dcontent"></div>
		
		<script src="js/mui.min.js"></script>
		<script src="js/app.js"></script>
		<script>
			mui.init();
			var cartItemId,
				skuIdCount,
				orderData,
				orderUrl,
				integral,
				couponIds=[],
				pays={},
				mask,
				payOrderId,
				ws;
			mui.plusReady(function() {
				ws=plus.webview.currentWebview();
				ws.setStyle({scrollIndicator:'none'});
				cartItemId=ws.cartItemId;
				skuIdCount=ws.skuIdCount;
				var w=plus.nativeUI.showWaiting('',{padlock:true});
				
				if(cartItemId!=null){
					orderData={cartItemIds:cartItemId};
					orderUrl='api/Order/GetSubmitByCartModel';
				}else if(skuIdCount!=null){
					orderData=JSON.parse(skuIdCount);
					orderUrl='api/Order/GetSubmitModel';
				}
				var yuanjia;
				mui.ajax(URL+orderUrl,{
					dataType:'json',
					data:orderData,
					type:'get',
					timeout:10000,
					success:function(data){
						console.log(JSON.stringify(data))
						if(data.Success=='true'){
							var html='';
							if(data.Address!=null){
								html+='<ul class="mui-table-view mui-table-view-chevron address-list" id="addressList" data-id="'+data.Address.AddrId+'">'+
									'<li class="mui-table-view-cell">'+
										'<a class="mui-navigate-right">'+
											'<p><span>'+data.Address.ShipTo+'</span>'+data.Address.Phone+'</p>'+
											'<p><i class="mui-icon mui-icon-location"></i><span>'+data.Address.Address+'</span></p>'+
											'<i class="mui-icon mui-icon-arrowright icon-right"></i>'+
										'</a>'+
									'</li>'+
								'</ul>';
							}else{
								html+='<ul class="mui-table-view mui-table-view-chevron address-list" id="addressList">'+
									'<li class="mui-table-view-cell">'+
										'<a class="mui-navigate-right">'+
											'<p>暂未添加您的专属收货地址，去设置</p>'+
											'<i class="mui-icon mui-icon-arrowright icon-right" style="top:0"></i>'+
										'</a>'+
									'</li>'+
								'</ul>';
							}
							for(var i=0;i<data.ShopCartItem.length;i++){
					            html+='<div class="order-info">'+
									'<div class="item">'+
										'<div class="order-shop">';
											if(data.ShopCartItem[i].vshopId==null)
												html+='<a>微店：'+data.ShopCartItem[i].ShopName+'</a>';
											else
												html+='<a class="vshop-url" data-id="'+data.ShopCartItem[i].vshopId+'">微店：'+data.ShopCartItem[i].ShopName+'</a>';
										html+='</div>'+
									'</div>';
					            	for(var j=0;j<data.ShopCartItem[i].CartItemModels.length;j++){
						                html+='<div class="item" data-skuid="'+data.ShopCartItem[i].CartItemModels[j].skuId+'">'+
						                    '<div class="buy-goods">'+
						                        '<a class="goods-url" data-id="'+data.ShopCartItem[i].CartItemModels[j].id+'"><img src="'+data.ShopCartItem[i].CartItemModels[j].imgUrl+'"/></a>'+
						                        '<p><em>¥ '+data.ShopCartItem[i].CartItemModels[j].price+'</em><span><a class="goods-url" data-id="'+data.ShopCartItem[i].CartItemModels[j].id+'">'+data.ShopCartItem[i].CartItemModels[j].name+'</a></span></p>'+
						                        '<h5><span id="count">x '+data.ShopCartItem[i].CartItemModels[j].count+'</span>';
						                        	if(data.ShopCartItem[i].CartItemModels[j].size!=null)
														html += '尺寸：'+data.ShopCartItem[i].CartItemModels[j].size+'&nbsp;&nbsp;';
													if(data.ShopCartItem[i].CartItemModels[j].color!=null)
														html += '颜色：'+data.ShopCartItem[i].CartItemModels[j].color+'&nbsp;&nbsp;';
													if(data.ShopCartItem[i].CartItemModels[j].version!=null)
														html += '规格：'+data.ShopCartItem[i].CartItemModels[j].version;
						                            html+='</h5>'+
						                    '</div>'+
						                '</div>';
						                 yuanjia=data.ShopCartItem[i].CartItemModels[j].price;
					               	}
					                if(data.ShopCartItem[i].Coupon!=null){
										couponIds.push(data.ShopCartItem[i].Coupon.Id+'_'+data.ShopCartItem[i].Coupon.BaseType);
										html+='<div class="item">'+
							                '<div class="item-text">'+
							                '<span>优惠券</span>'+
							                '<span class="mui-pull-right">抵扣 &nbsp;'+data.ShopCartItem[i].Coupon.BasePrice+'元</span></div>'+
							            '</div>';
									}
						            html+='<div class="item">'+
						                '<div class="item-text">'+
						                '<span>配送方式</span>';
						                if(data.ShopCartItem[i].isFreeFreight)
						                	html+='<span class="mui-pull-right">快递 &nbsp;免邮</span></div>';
						                else
						                	html+='<span class="mui-pull-right">快递 &nbsp;'+data.ShopCartItem[i].Freight+'元</span></div>';
						            html+='</div>'+
						            '<div class="item total-m">'+
					                    '&nbsp;<span class="mui-pull-right total">微店合计<em>¥ '+data.ShopCartItem[i].OrderAmount+'</em></span>'+
						            '</div>'+
						        '</div>';
					       	}

//							if(data.UserIntegrals<1||yuanjia<1)
//							{
//							 html+='<div class="order-info" style="display:none">'+
//								'<div class="item">'+
//									'<div class="item-score">'+
//									    '<span>可用'+data.UserIntegrals+'金豆抵扣<em>¥'+data.UserIntegrals+'</em></span>'+
//									    '<div id="useIntegral" data-score="'+data.UserIntegrals+'" class="mui-switch mui-active mui-pull-right"><div class="mui-switch-handle"></div></div>'+
//									'</div>'+
//								'</div>'+
//							'</div>';
//							}else{
//					        html+='<div class="order-info">'+
//								'<div class="item">'+
//									'<div class="item-score">'+
//									    '<span>可用'+data.UserIntegrals+'金豆抵扣<em>¥'+data.UserIntegrals+'</em></span>'+
//									    '<div id="useIntegral" data-score="'+data.UserIntegrals+'" class="mui-switch mui-active mui-pull-right"><div class="mui-switch-handle"></div></div>'+
//									'</div>'+
//								'</div>'+
//							'</div>';
//							}
//							integral=data.UserIntegrals;
							var OrderAmount=document.getElementById('OrderAmount');
							document.getElementById('orderSubmit').innerHTML=html;
							//总金额
//							OrderAmount.innerText='¥ '+((parseFloat(data.OrderAmount)-parseFloat(integral))).toFixed(2);
							OrderAmount.innerText='¥ '+data.OrderAmount;
							document.getElementById('OrderCount').innerText=document.getElementsByClassName('buy-goods').length;
							
//							mui('#useIntegral').switch();
//							mui('#orderSubmit').on('toggle','#useIntegral',function(event){
//								setTimeout(function() {
//									if(event.detail.isActive){
//										integral=data.UserIntegrals;
//										OrderAmount.innerText='¥ '+((parseFloat(data.OrderAmount)-parseFloat(integral))).toFixed(2);
//									}else{
//										integral=0;
//										OrderAmount.innerText='¥ '+data.OrderAmount;
//									}
//								}, 50);
//							});
							
				            w.close();
				            document.getElementsByClassName("mui-content")[0].className="mui-content";
				            document.getElementsByClassName("cart-bottom")[0].className="cart-bottom";
				        }
					},
					error:function(xhr,type,errorThrown){
//						console.log(JSON.stringify(xhr))
						w.close();
						reloadWvLoad();
					}
				});
				
				plus.payment.getChannels(function(channels){
					var content=document.getElementById('dcontent');
					for(var i in channels){
						var channel=channels[i];
						if(channel.id=='alipay' /*|| channel.id=='wxpay'*/){
							pays[channel.id]=channel;
							var de=document.createElement('div');
							de.setAttribute('class','custom-btn');
							de.setAttribute('onclick','pay(this.id)');
							de.id=channel.id;
							de.innerText=channel.description+"支付";
							content.appendChild(de);
							checkServices(channel);
						}
					}
				},function(e){
					plus.nativeUI.alert("获取支付方式失败："+e.message);
				});
				
			});
			
			mui('#orderSubmit').on('tap', '.goods-url', function() {
				var id = this.getAttribute('data-id');
				showProduct(id);
			});
			
			mui('#orderSubmit').on('tap', '.vshop-url', function() {
				var vshopid = this.getAttribute('data-id');
				showVshop(vshopid,'/')
			});
			
			mui('#orderSubmit').on('tap', '#addressList', function() {
				mui.openWindow({
					id:'address.html',
					url:'user/address.html',
					show: {
						autoShow:true,
						aniShow: 'pop-in'
					},
					extras:{
						orderTag:'orderTag'
				    },
					waiting: {
						autoShow: false
					}
				});
			});
			
			window.addEventListener('updateAdress',function(event){
				var addressList=document.getElementById('addressList'),
					newAdressId = event.detail.newAdressId,
					newAdress= event.detail.newAdress;
				addressList.setAttribute('data-id',newAdressId);
				addressList.innerHTML='<li class="mui-table-view-cell">'+
					'<a class="mui-navigate-right">'+newAdress+'</a>'+
				'</li>';
				document.getElementsByClassName('mui-icon-compose')[0].className='mui-icon mui-icon-arrowright icon-right';
				document.getElementsByClassName('mui-icon-arrowright')[0].previousSibling.insertAdjacentHTML('afterbegin','<i class="mui-icon mui-icon-location"></i>');
			});
			
			document.getElementById('submitBtn').addEventListener('tap',function(){
				var submitUrl,
					submitData,
					couponId=couponIds.join(','),
					recieveAddressId=document.getElementById('addressList').getAttribute('data-id');
				if(!recieveAddressId){
					plus.nativeUI.toast('请设置收货地址');
					return false;
				}
				var w=plus.nativeUI.showWaiting('',{padlock:true});
				plus.webview.currentWebview('')
				if(cartItemId!=null){
					submitUrl='api/Order/PostSubmitOrderByCart';
					submitData={
						cartItemIds:cartItemId,
						recieveAddressId:recieveAddressId,
						couponIds:couponId,
						integral:0,
					};
				}else if(skuIdCount!=null){
					submitUrl='api/Order/PostSubmitOrder';
					submitData={
						skuIds:orderData.skuId,
						counts:orderData.count,
						recieveAddressId:recieveAddressId,
						couponIds:couponId,
						integral:0,
					};
				}
				mui.ajax(URL+submitUrl,{
					data:JSON.stringify(submitData),
					dataType:'json',
					contentType:'application/json',
					type:'POST',
					timeout:10000,
					success:function(data){
//						console.log(data)
						if(data.Success=="true"){
							plus.nativeUI.toast('订单已提交，请尽快付款');
							if(cartItemId!=null)
								mui.fire(plus.webview.getWebviewById('cart.html'),'updateData');
							payOrderId=data.OrderIds.join(',');

							if(data.RealTotalIsZero){
									plus.nativeUI.alert('购买成功，等待发货',function(){
												openOrderList(2);
											});
							
//							if(parseFloat(document.getElementById('OrderAmount').innerText.replace('¥ ',''))<=0){
	
//								mui.ajax(URL+'api/Order/GetPayOrderByIntegral?orderIds='+payOrderId,{
//									dataType:'json',
//									type:'get',
//									timeout:10000,
//									success:function(data){
//										if(data.Success=="true"){
//											w.close();
//											plus.nativeUI.alert('购买成功，等待发货',function(){
//												openOrderList(3);
//											});
//											
//										}else{
//											w.close();
//											plus.nativeUI.toast('订单支付失败，请稍后重试')
//										}
//									},
//									error:function(xhr,type,errorThrown){
//										w.close();
//										plus.nativeUI.toast('订单支付失败，请检查网络')
//									}
//								});
								
							}else{
								w.close();
								if(!mask)
									mask = mui.createMask(function(){
										document.getElementById('dcontent').className='dcontent';
										openOrderList(0);
									});
								mask.show();
								document.getElementById('dcontent').className='dcontent active';
							}
						}else{
							w.close();
							plus.nativeUI.alert(data.Msg);
						}
					},
					error:function(xhr,type,errorThrown){
//						console.log(URL+submitUrl)
						w.close();
						plus.nativeUI.toast('提交订单失败，请检查网络')
					}
				});
				
				
			});
			
			// 检测是否安装支付服务
			function checkServices(pc){
				if(!pc.serviceReady){
					var txt=null;
					switch(pc.id){
						case "alipay":
						txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
						break;
						default:
						txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
						break;
					}
					plus.nativeUI.confirm(txt,function(e){
						if(e.index==0){
							pc.installService();
						}
					},pc.description);
				}
			}
			
			var wPay=null;
			// 支付
			function pay(id){
				if(wPay){return;}//检查是否请求订单中
				if(id=='alipay'||id=='wxpay'){
					//url+=id;
				}else{
					plus.nativeUI.alert("不支持此支付通道！");
					return;
				}
				
				wPay=plus.nativeUI.showWaiting();
				// 请求支付订单
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					switch(xhr.readyState){
						case 4:
						wPay.close();wPay=null;
						if(xhr.status==200){
							var order=JSON.parse(xhr.responseText);
							//没进这里
							
							plus.payment.request(pays[id],order[0].url,
							function(result){
								plus.nativeUI.alert("支付成功，等待卖家发货！",function(){

								});
							},
							function(e){
								plus.nativeUI.alert("订单支付失败！"+e.code,function(){
									

								});
							});
							
						}else{
							plus.nativeUI.alert("获取订单信息失败！");
						}
						break;
						default:
						break;
					}
				}
				xhr.open('GET',URL+'api/payment/GetPaymentList?orderIds='+payOrderId);
				xhr.send();
			}
			
			function openOrderList(canshu){
				mui.openWindow({
					id:'order-list.html',
					url:'order-list.html',
					extras:{
						orderStatus:canshu
				    },
					show: {
						autoShow:true,
						aniShow: 'pop-in'
					},
					waiting: {
						autoShow: false
					}
				});
				var submitPage=plus.webview.getWebviewById('order-submit.html');
				setTimeout(function(){
					submitPage.hide();
					submitPage.close();
				},2000)
			}
			
		</script>
	</body>

</html>